package com.example.pxxy_news.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.pxxy_news.R;
import com.example.pxxy_news.db.DBHelper;
import com.example.pxxy_news.entity.User;
import com.google.gson.Gson;
import okhttp3.*;

import java.io.IOException;

public class LoginActivity extends BaseActivity {
	public static final String URL_LOGIN = "http://10.102.41.48:8080/news_server/login";
	private DBHelper helper = null;
	private SQLiteDatabase sd = null;
	private EditText et_name, et_password;
	private Button btn_login;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
		autoLogin();
	}

	private void autoLogin() {
		Log.d("news", "��ʼִ���Զ���¼");
		sp = getSharedPreferences("user", MODE_PRIVATE);
		String name = sp.getString("username", "");
		String password = sp.getString("password", "");
		Log.d("news", name);
		Log.d("news", password);
		if (!name.equals("") && !password.equals("")) {
			Toast.makeText(this, "�Զ���¼�ɹ�", 1).show();
			Intent in = new Intent(LoginActivity.this, HomeActivity.class);
			startActivity(in);
			closeActivity();
		}
	}
	
	private void closeActivity() {
		this.finish();
	}
	
	private void initView() {
		et_name = (EditText) findViewById(R.id.et_name);
		et_password = (EditText) findViewById(R.id.et_password);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String name = et_name.getText().toString();
				String password = et_password.getText().toString();

				OkHttpClient client = new OkHttpClient();
				RequestBody body = new FormBody.Builder()
									.add("username", name)
									.add("password", password)
									.build();
				Request request = new Request.Builder()
									.url(URL_LOGIN)
									.post(body)
									.build();
				
				client.newCall(request).enqueue(new Callback() {
					@Override
					public void onResponse(Call arg0, Response res) throws IOException {
						//Looper.prepare();
						String result = res.body().string();
						Log.d("news", result);
						doLogin(result);
					}
				
					@Override
					public void onFailure(Call arg0, IOException arg1) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});
	}
	
	private void doLogin(String result) {
		if(result.equals("0")){
			Looper.prepare();
			Toast.makeText(this, "�˺Ż��������", 1).show();
			Looper.loop();
		}else{
			Gson gson = new Gson();
			User user = gson.fromJson(result, User.class);
			String userName = user.getUsername();
			String userPass = user.getPassword();
			int _id = user.get_id();
			Log.d("news", _id+"");
			Log.d("news", userName);
			Log.d("news", userPass);
			
			sp = getSharedPreferences("user", MODE_PRIVATE);
			Editor editor = sp.edit();
			editor.putString("username", userName);
			editor.putString("password", userPass);
			editor.putInt("_id",_id);
			editor.commit();
			Intent in = new Intent(LoginActivity.this,HomeActivity.class);
			startActivity(in);
			closeActivity();
		}
		
	}
}
